{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "import pandas as pd\n",
    "import numpy as np"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "###  时间序列 ###\n",
    "- 时间戳（timestamp）\n",
    "- 固定周期（period）\n",
    "- 时间间隔（interval）\n",
    "\n",
    "<img src=\"f1.png\" alt=\"FAO\" width=\"590\" >"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### date_range ###\n",
    "- 可以指定开始时间与周期\n",
    "- H：小时\n",
    "- D：天\n",
    "- M：月"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 58,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "DatetimeIndex(['2016-07-01', '2016-07-04', '2016-07-07', '2016-07-10',\n",
       "               '2016-07-13', '2016-07-16', '2016-07-19', '2016-07-22',\n",
       "               '2016-07-25', '2016-07-28'],\n",
       "              dtype='datetime64[ns]', freq='3D')"
      ]
     },
     "execution_count": 58,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# TIMES #2016 Jul 1 7/1/2016 1/7/2016 2016-07-01 2016/07/01\n",
    "rng = pd.date_range('2016-07-01', periods = 10, freq = '3D')\n",
    "rng"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 26,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "2016-01-01   -0.129379\n",
      "2016-01-02    0.164480\n",
      "2016-01-03   -0.639117\n",
      "2016-01-04   -0.427224\n",
      "2016-01-05    2.055133\n",
      "2016-01-06    1.116075\n",
      "2016-01-07    0.357426\n",
      "2016-01-08    0.274249\n",
      "2016-01-09    0.834405\n",
      "2016-01-10   -0.005444\n",
      "2016-01-11   -0.134409\n",
      "2016-01-12    0.249318\n",
      "2016-01-13   -0.297842\n",
      "2016-01-14   -0.128514\n",
      "2016-01-15    0.063690\n",
      "2016-01-16   -2.246031\n",
      "2016-01-17    0.359552\n",
      "2016-01-18    0.383030\n",
      "2016-01-19    0.402717\n",
      "2016-01-20   -0.694068\n",
      "Freq: D, dtype: float64\n"
     ]
    }
   ],
   "source": [
    "time=pd.Series(np.random.randn(20),\n",
    "           index=pd.date_range(dt.datetime(2016,1,1),periods=20))\n",
    "print(time)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "###  truncate过滤 ###"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 50,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "2016-01-10   -0.005444\n",
       "2016-01-11   -0.134409\n",
       "2016-01-12    0.249318\n",
       "2016-01-13   -0.297842\n",
       "2016-01-14   -0.128514\n",
       "2016-01-15    0.063690\n",
       "2016-01-16   -2.246031\n",
       "2016-01-17    0.359552\n",
       "2016-01-18    0.383030\n",
       "2016-01-19    0.402717\n",
       "2016-01-20   -0.694068\n",
       "Freq: D, dtype: float64"
      ]
     },
     "execution_count": 50,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "time.truncate(before='2016-1-10')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 51,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "2016-01-01   -0.129379\n",
       "2016-01-02    0.164480\n",
       "2016-01-03   -0.639117\n",
       "2016-01-04   -0.427224\n",
       "2016-01-05    2.055133\n",
       "2016-01-06    1.116075\n",
       "2016-01-07    0.357426\n",
       "2016-01-08    0.274249\n",
       "2016-01-09    0.834405\n",
       "2016-01-10   -0.005444\n",
       "Freq: D, dtype: float64"
      ]
     },
     "execution_count": 51,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "time.truncate(after='2016-1-10')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 27,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "0.063690487247\n"
     ]
    }
   ],
   "source": [
    "print(time['2016-01-15'])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 28,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "2016-01-15    0.063690\n",
      "2016-01-16   -2.246031\n",
      "2016-01-17    0.359552\n",
      "2016-01-18    0.383030\n",
      "2016-01-19    0.402717\n",
      "2016-01-20   -0.694068\n",
      "Freq: D, dtype: float64\n"
     ]
    }
   ],
   "source": [
    "print(time['2016-01-15':'2016-01-20'])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 29,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "DatetimeIndex(['2010-01-31', '2010-02-28', '2010-03-31', '2010-04-30',\n",
      "               '2010-05-31', '2010-06-30', '2010-07-31', '2010-08-31',\n",
      "               '2010-09-30', '2010-10-31', '2010-11-30', '2010-12-31'],\n",
      "              dtype='datetime64[ns]', freq='M')\n"
     ]
    }
   ],
   "source": [
    "data=pd.date_range('2010-01-01','2011-01-01',freq='M')\n",
    "print(data)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "<img src=\"f2.png\" alt=\"FAO\" width=\"590\" >"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 17,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "Timestamp('2016-07-10 00:00:00')"
      ]
     },
     "execution_count": 17,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "#时间戳\n",
    "pd.Timestamp('2016-07-10')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 18,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "Timestamp('2016-07-10 10:00:00')"
      ]
     },
     "execution_count": 18,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 可以指定更多细节\n",
    "pd.Timestamp('2016-07-10 10')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 19,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "Timestamp('2016-07-10 10:15:00')"
      ]
     },
     "execution_count": 19,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "pd.Timestamp('2016-07-10 10:15')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "# How much detail can you add?"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": false
   },
   "outputs": [],
   "source": [
    "t = pd.Timestamp('2016-07-10 10:15')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 30,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "Period('2016-01', 'M')"
      ]
     },
     "execution_count": 30,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 时间区间\n",
    "pd.Period('2016-01')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 21,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "Period('2016-01-01', 'D')"
      ]
     },
     "execution_count": 21,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "pd.Period('2016-01-01')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 35,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "Timedelta('1 days 00:00:00')"
      ]
     },
     "execution_count": 35,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# TIME OFFSETS\n",
    "pd.Timedelta('1 day')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 36,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "Period('2016-01-02 10:10', 'T')"
      ]
     },
     "execution_count": 36,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "pd.Period('2016-01-01 10:10') + pd.Timedelta('1 day')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 37,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "Timestamp('2016-01-02 10:10:00')"
      ]
     },
     "execution_count": 37,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "pd.Timestamp('2016-01-01 10:10') + pd.Timedelta('1 day')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 38,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "Timestamp('2016-01-01 10:10:00.000000015')"
      ]
     },
     "execution_count": 38,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "pd.Timestamp('2016-01-01 10:10') + pd.Timedelta('15 ns')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 39,
   "metadata": {
    "collapsed": false
   },
   "outputs": [],
   "source": [
    "p1 = pd.period_range('2016-01-01 10:10', freq = '25H', periods = 10)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 40,
   "metadata": {
    "collapsed": false
   },
   "outputs": [],
   "source": [
    "p2 = pd.period_range('2016-01-01 10:10', freq = '1D1H', periods = 10)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 41,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "PeriodIndex(['2016-01-01 10:00', '2016-01-02 11:00', '2016-01-03 12:00',\n",
       "             '2016-01-04 13:00', '2016-01-05 14:00', '2016-01-06 15:00',\n",
       "             '2016-01-07 16:00', '2016-01-08 17:00', '2016-01-09 18:00',\n",
       "             '2016-01-10 19:00'],\n",
       "            dtype='period[25H]', freq='25H')"
      ]
     },
     "execution_count": 41,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "p1"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 42,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "PeriodIndex(['2016-01-01 10:00', '2016-01-02 11:00', '2016-01-03 12:00',\n",
       "             '2016-01-04 13:00', '2016-01-05 14:00', '2016-01-06 15:00',\n",
       "             '2016-01-07 16:00', '2016-01-08 17:00', '2016-01-09 18:00',\n",
       "             '2016-01-10 19:00'],\n",
       "            dtype='period[25H]', freq='25H')"
      ]
     },
     "execution_count": 42,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "p2"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 43,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "2016-07-01    0\n",
       "2016-07-02    1\n",
       "2016-07-03    2\n",
       "2016-07-04    3\n",
       "2016-07-05    4\n",
       "2016-07-06    5\n",
       "2016-07-07    6\n",
       "2016-07-08    7\n",
       "2016-07-09    8\n",
       "2016-07-10    9\n",
       "Freq: D, dtype: int32"
      ]
     },
     "execution_count": 43,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 指定索引\n",
    "rng = pd.date_range('2016 Jul 1', periods = 10, freq = 'D')\n",
    "rng\n",
    "pd.Series(range(len(rng)), index = rng)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 44,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "2016-01   -0.015837\n",
       "2016-02   -0.923463\n",
       "2016-03   -0.485212\n",
       "Freq: M, dtype: float64"
      ]
     },
     "execution_count": 44,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "periods = [pd.Period('2016-01'), pd.Period('2016-02'), pd.Period('2016-03')]\n",
    "ts = pd.Series(np.random.randn(len(periods)), index = periods)\n",
    "ts"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 45,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "pandas.core.indexes.period.PeriodIndex"
      ]
     },
     "execution_count": 45,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "type(ts.index)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 46,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "2016-07-10 08:00:00    0\n",
       "2016-07-10 09:00:00    1\n",
       "2016-07-10 10:00:00    2\n",
       "2016-07-10 11:00:00    3\n",
       "2016-07-10 12:00:00    4\n",
       "2016-07-10 13:00:00    5\n",
       "2016-07-10 14:00:00    6\n",
       "2016-07-10 15:00:00    7\n",
       "2016-07-10 16:00:00    8\n",
       "2016-07-10 17:00:00    9\n",
       "Freq: H, dtype: int32"
      ]
     },
     "execution_count": 46,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 时间戳和时间周期可以转换\n",
    "ts = pd.Series(range(10), pd.date_range('07-10-16 8:00', periods = 10, freq = 'H'))\n",
    "ts"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 47,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "2016-07-10 08:00    0\n",
       "2016-07-10 09:00    1\n",
       "2016-07-10 10:00    2\n",
       "2016-07-10 11:00    3\n",
       "2016-07-10 12:00    4\n",
       "2016-07-10 13:00    5\n",
       "2016-07-10 14:00    6\n",
       "2016-07-10 15:00    7\n",
       "2016-07-10 16:00    8\n",
       "2016-07-10 17:00    9\n",
       "Freq: H, dtype: int32"
      ]
     },
     "execution_count": 47,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "ts_period = ts.to_period()\n",
    "ts_period"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 48,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "2016-07-10 08:00    0\n",
       "2016-07-10 09:00    1\n",
       "2016-07-10 10:00    2\n",
       "2016-07-10 11:00    3\n",
       "Freq: H, dtype: int32"
      ]
     },
     "execution_count": 48,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "ts_period['2016-07-10 08:30':'2016-07-10 11:45'] "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 49,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "2016-07-10 09:00:00    1\n",
       "2016-07-10 10:00:00    2\n",
       "2016-07-10 11:00:00    3\n",
       "Freq: H, dtype: int32"
      ]
     },
     "execution_count": 49,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "ts['2016-07-10 08:30':'2016-07-10 11:45'] "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "anaconda-cloud": {},
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.5.2"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 0
}
